;import { Component, Vue,Inject } from 'vue-property-decorator';
import appconst from './appconst';
import Ajax from './ajax';
import moment from 'moment';
import Util from './util';

export default class AbpBase extends Vue{
    showPage:boolean = true;
    appApiBaseUrl:String = "/api/services/app/";
    L(value:string,source?:string,...argus:string[]):string{
        if(source){
            return window.abp.localization.localize(value,source,argus);
        }else{
            return window.abp.localization.localize(value,appconst.localization.defaultLocalizationSourceName,argus);
        }
        
    }
    hasPermission(permissionName:string){
        return window.abp.auth.hasPermission(permissionName);
    }
    hasAnyOfPermissions(...argus:string[]){
        return window.abp.auth.hasAnyOfPermissions(...argus);
    }
    hasAllOfPermissions(...argus:string[]){
        return window.abp.auth.hasAllOfPermissions(...argus);
    }

    get isPhone() {
        return this.$store.state.app.isPhone;
    }

	get isLogined():Boolean {
		return Util.abp.session.userId != null && sessionStorage.getItem('userType') == 'member';
	}

    async post(url:string, data?:any) {
        let response = await Ajax.post( this.appApiBaseUrl + url, data );
        return response.data;
    }
    
    async put(url:string, data?:any) {
        let response = await Ajax.put( this.appApiBaseUrl + url, data );
        return response.data;
    }

    async delete(url:string, data?:any) {
        let response = await Ajax.delete( this.appApiBaseUrl + url, {
            params: data
        } );
        return response.data;
    }
    
    async get(url:string, data?:any) {
        let response = await Ajax.get( this.appApiBaseUrl + url, {
            params: data
        } );
        return response.data;
    }

    confirm(content:string, okFunc:Function) {
        this.$Modal.confirm({
            title: '提示',
            content: content,
            okText: '确认',
            cancelText: '取消',
            onOk:async()=>{
                okFunc();
            }
        })
    }

    error(content:string) {
        this.$Modal.error({
            title: '错误',
            content: content,
            okText: '确认',
            cancelText: '取消'
        })
    }

    success(content:string) {
        this.$Modal.success({
            title: '成功',
            content: content,
            okText: '确认',
            cancelText: '取消'
        })
    }

    viewPicture(picture:string) {
        this.$Modal.info({
            footerHide: true,
            width: 80,
            maskClosable: true,
            render: (h) => {
                return h('div', {
                    style: { textAlign: 'center' }
                }, [
                    h('img', {
                        attrs: { src: picture },
                        style: { maxWidth: '100%' }
                    })
                ])
            }
        });
    }

    formatTimeStr(timeStr:string, format?:string) {
        return moment(new Date(timeStr)).format(format ? format : 'YYYY-MM-DD HH:mm:ss');
    }

    formatPrice(price:Number){
        if(price == undefined) {
            return "0.00";
        }else {
            return price.toFixed(2);
        }
    }

    getRandomRef(prefix:string) {
        return prefix + '-' + Date.now();
    }

    ellipsis(str:string, length:number) {
        if(!str) return "";
        if(!length) length = 50;
        if(str.length > length - 2) {
            return str.substring(0, length) + '...';
        }else {
            return str;
        }
    }

    validate(ref:string, callback:Function, errorCallback?:Function) {
        (this.$refs[ref] as any).validate( valid => {
            if(valid) {
                callback();
            }else {
                if(errorCallback) errorCallback();
            }
        })
    }
}